﻿<!DOCTYPE html>

<html lang="ru" xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta charset="utf-8" />
        <!--//TODO: эта фраза встречается два раза-->
        <title>Архитектура vs кодирование</title>
        <link rel="stylesheet" href="app.css" type="text/css" />
    </head>
    <body>
        <h1>Архитектура vs кодирование</h1>
        <p>
            Так пишет
            <a href="http://martinfowler.com/articles/dblogic.html#DomainModel">архитектор</a>:
        </p>
        <pre>class CustomerMapper
  def find name
    result = nil
    sql = 'SELECT * FROM customers WHERE name = ?'
    return load($dbh.select_one(sql, name)) 
  end
  def load row
    result = Customer.new(row['customerID'], row['NAME'])
    result.orders = OrderMapper.new.find_for_customer result
    return result
  end
end

class OrderMapper
  def find_for_customer aCustomer
    result = []
    sql = "SELECT * FROM orders WHERE customerID = ?" 
    $dbh.select_all(sql, aCustomer.db_id) {|row| result << load(row)}
    load_line_items result
    return result
  end
  def load row
    result = Order.new(row['orderID'], row['date'])
    return result  
  end
  def load_line_items orders
    #Cannot load with load(row) as connection gets busy
    orders.each do
      |anOrder| anOrder.line_items = LineItemMapper.new.find_for_order anOrder
    end
  end
end

class LineItemMapper
  def find_for_order order
    result = []
    sql = "select * from lineItems where orderID = ?"
    $dbh.select_all(sql, order.db_id) {|row| result << load(row)}
    return result
  end
  def load row
    return LineItem.new(row['lineNumber'], row['product'], row['cost'].to_i.dollars)
  end
end
class Customer...
  attr_accessor :name, :db_id, :orders
  def initialize db_id, name
    @db_id, @name = db_id, name
  end

class Order...
  attr_accessor :date, :db_id, :line_items
  def initialize (id, date)
    @db_id, @date, @line_items = id, date, []
  end

class LineItem...
  attr_reader :line_number, :product, :cost
  def initialize line_number, product, cost
    @line_number, @product, @cost = line_number, product, cost
  end
            
class Customer...
  def cuillenMonths
    result = []
    orders.each do |o|
      result << o.date.month if o.cuillen?
    end
    return result.uniq
  end

class Order...
  def cuillen?
    discountableAmount = 0.dollars
    line_items.each do |line| 
      discountableAmount += line.cost if 'Talisker' == line.product
    end
    return discountableAmount > 5000.dollars
  end</pre>
    </body>
    Так пишет кодер:
    <pre>static IEnumerable<int> CuillenMonths(string companyName)
        {
            var customerID = Query.Create<int>(
                "SELECT CustomerID FROM Customer WHERE CompanyName = @companyName",
                new {companyName}).GetEnumerable().Single();
            return Query.Create<T001>(
                "SELECT OrderID, OrderDate FROM Order WHERE CustomerID = @customerID",
                new {customerID}).GetEnumerable()
                .Where(IsCuillen)
                .Select(order => order.OrderDate.Month)
                .Distinct();
        }

        static bool IsCuillen(T001 order)
        {
            return Query.Create<T002>(
                "SELECT ProductID FROM OrderLine WHERE OrderID = @OrderID",
                new {order.OrderID}).GetEnumerable()
                .Where(line => line.ProductID == 2702)
                .Sum(line => line.Quantity) > 14;
        }</pre>
</html>